﻿@model AffiliateListModel
    @{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.Affiliates").Text;
}
@Html.AntiForgeryToken()
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-promotions.png")" alt="" />
        @T("Admin.Affiliates")
    </div>
    <div class="options">
        <a href="@Url.Action("Create")" class="k-button">@T("Admin.Common.AddNew")</a>
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchFirstName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.SearchFirstName)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchLastName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.SearchLastName)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchFriendlyUrlName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.SearchFriendlyUrlName)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.LoadOnlyWithOrders):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.LoadOnlyWithOrders)
        </td>
    </tr>
    <tr id="pnlOrdersCreatedFromUtc">
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.OrdersCreatedFromUtc):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.OrdersCreatedFromUtc)
        </td>
    </tr>
    <tr id="pnlOrdersCreatedToUtc">
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.OrdersCreatedToUtc):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.OrdersCreatedToUtc)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-affiliates" class="k-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<script type="text/javascript">
    $(document).ready(function () {
        $("#@Html.FieldIdFor(model => model.LoadOnlyWithOrders)").click(toggleLoadOnlyWithOrders);

        toggleLoadOnlyWithOrders();
    });

    function toggleLoadOnlyWithOrders() {
        if ($('#@Html.FieldIdFor(model => model.LoadOnlyWithOrders)').is(':checked')) {
            $('#pnlOrdersCreatedFromUtc').show();
            $('#pnlOrdersCreatedToUtc').show();
        } else {
            $('#pnlOrdersCreatedFromUtc').hide();
            $('#pnlOrdersCreatedToUtc').hide();
        }
    }
</script>

<table class="adminContent">
    <tr>
        <td>
            <div id="affiliates-grid"></div>

            <script>
                    $(document).ready(function () {
                        $("#affiliates-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("List", "Affiliate"))",
                                        type: "POST",
                                        dataType: "json",
                                        data: additionalData
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors"
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                pageSize: @(defaultGridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                pageSizes: [@(gridPageSizes)]
                            },
                            editable: {
                                confirmation: false,
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "Address.FirstName",
                                title: "@T("Admin.Address.Fields.FirstName")",
                                width: 200
                            }, {
                                field: "Address.LastName",
                                title: "@T("Admin.Address.Fields.LastName")",
                                width: 200
                            }, {
                                field: "Active",
                                title: "@T("Admin.Affiliates.Fields.Active")",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=Active#.gif" />'
                            }, {
                                field: "Id",
                                title: "@T("Admin.Common.Edit")",
                                width: 100,
                                template: '<a href="Edit/#=Id#">@T("Admin.Common.Edit")</a>'
                            }]
                        });
                    });
            </script>
            <script type="text/javascript">
                $(document).ready(function () {

                    $('#search-affiliates').click(function () {
                        var grid = $('#affiliates-grid').data('kendoGrid');
                        grid.dataSource.page(1); //new search. Set page size to 1
                        //grid.dataSource.read(); we already loaded the grid above using "page" function
                        return false;
                    });
                });

                $("#@Html.FieldIdFor(model => model.SearchFirstName)").keydown(function (event) {
                    if (event.keyCode == 13) {
                        $("#search-affiliates").click();
                        return false;
                    }
                });
                $("#@Html.FieldIdFor(model => model.SearchLastName)").keydown(function (event) {
                    if (event.keyCode == 13) {
                        $("#search-affiliates").click();
                        return false;
                    }
                });
                $("#@Html.FieldIdFor(model => model.SearchFriendlyUrlName)").keydown(function (event) {
                    if (event.keyCode == 13) {
                        $("#search-affiliates").click();
                        return false;
                    }
                });

                function additionalData() {
                    var data = {
                        SearchFirstName: $('#@Html.FieldIdFor(model => model.SearchFirstName)').val(),
                        SearchLastName: $('#@Html.FieldIdFor(model => model.SearchLastName)').val(),
                        SearchFriendlyUrlName: $('#@Html.FieldIdFor(model => model.SearchFriendlyUrlName)').val(),
                        LoadOnlyWithOrders: $('#@Html.FieldIdFor(model => model.LoadOnlyWithOrders)').is(':checked'),
                        OrdersCreatedFromUtc: $('#@Html.FieldIdFor(model => model.OrdersCreatedFromUtc)').val(),
                        OrdersCreatedToUtc: $('#@Html.FieldIdFor(model => model.OrdersCreatedToUtc)').val()
                    };
                    addAntiForgeryToken(data);
                    return data;
                }

            </script>
        </td>
    </tr>
</table>